package org.kunlong.qgen.service.impl;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.kunlong.qgen.common.util.ConfigurationManager;
import org.kunlong.qgen.domain.QuestionHolder;
import org.kunlong.qgen.parser.Parser;
import org.kunlong.qgen.service.AbstractMultiThreadsQuestionLoadService;

public class DefaultMultiThreadLoadService extends
		AbstractMultiThreadsQuestionLoadService {
	@SuppressWarnings("unchecked")
	@Override
	public void parse(File file, QuestionHolder questionHolder)
			throws Exception {
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(file);
		Element questionsRoot = document.getRootElement();
		List<Element> questions = questionsRoot.elements("Question");
		for (Element e : questions) {
			String type = e.attribute("type").getText().trim();
			if (null == type || "".equals(type)) {
				//do nothing and go to next question node
				continue;
			}
			Parser parser = ConfigurationManager.getConfiguration()
					.getQuestionType().get(type).getParser();
			try {
				parser.parse(e, questionHolder);
			} catch (Exception ex) {
				//any problem with the question, forget about it...
				ex.printStackTrace();
				continue;
			}
		}
	}
}
